﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using OSSUtility;
using OSSUtility.Device;

namespace Ozone.Device
{

    /// <summary>
    /// 蠕动泵
    /// </summary>
    public class DevPeristalticPump : DevBase
    {
        //COM
        public DevPeristalticPump(int CommType, string FSrvIP, int FPort) : base(CommType, FSrvIP, FPort)
        {
        }


        private int nPumpType = 0;


        public void setPumpType(int PumpType)
        {
            nPumpType = PumpType;
        }

        public bool setFlow(double flow)
        {
            bool lockTaken = false;
            try
            {
                Monitor.TryEnter(base.objLock, 5000, ref lockTaken);
                if (lockTaken)
                {
                    double num = flow;
                    string text = Utility.padStr(((nPumpType != 0) ? (10.0 * num) : (1000.0 * num)).ToString(), 0, 5, '0');
                    string strCmd = "P0,S3," + text + "\r\n";
                    string text2 = base.session_.sendAndReadStr(strCmd);
                    if (text2.Contains("H2"))
                    {
                        return true;
                    }

                    return false;
                }

                return false;
            }
            catch (Exception ex)
            {
                Log.writeLog("DevBP-setPress", ex.Message);
                return false;
            }
            finally
            {
                if (lockTaken)
                {
                    Monitor.Exit(base.objLock);
                }
            }
        }

        public bool Switch(bool bFlag)
        {
            bool lockTaken = false;
            try
            {
                Monitor.TryEnter(base.objLock, 5000, ref lockTaken);
                if (lockTaken)
                {
                    string text = "0";
                    if (bFlag)
                    {
                        text = "1";
                    }

                    string text2 = base.session_.sendAndReadStr("P0,G1," + text + "\r\n");
                    if (text2.Contains("H2"))
                    {
                        return true;
                    }

                    return false;
                }

                return false;
            }
            catch (Exception ex)
            {
                Log.writeLog("DevBP-getRealData", ex.Message + " strRet ==== \r\n");
                return false;
            }
            finally
            {
                if (lockTaken)
                {
                    Monitor.Exit(base.objLock);
                }
            }
        }

        public bool getRealData(out string retSwitchFlag, out string retFlow, out string retPress)
        {
            string text = "";
            retPress = "";
            retFlow = "";
            retSwitchFlag = "";
            bool lockTaken = false;
            try
            {
                Monitor.TryEnter(base.objLock, 5000, ref lockTaken);
                if (lockTaken)
                {
                    string text2 = base.session_.sendAndReadStr("P0,Q2\r\n");
                    string[] array = text2.Split(',');
                    if (array.Length != 6)
                    {
                        return false;
                    }

                    if (array[2] == "1")
                    {
                        retSwitchFlag = "0";
                    }
                    else
                    {
                        retSwitchFlag = "1";
                    }

                    double num = Utility.str2double(array[5]) / 10.0;
                    string str = array[4];
                    double num2 = Utility.str2double(str);
                    str = ((nPumpType != 0) ? (num2 / 10.0).ToString("#0.0") : (num2 / 1000.0).ToString("#0.000"));
                    retPress = num.ToString();
                    retFlow = str;
                    return true;
                }

                return false;
            }
            catch (Exception ex)
            {
                Log.writeLog("DevBP-getRealData", ex.Message + " strRet ==== " + text + "\r\n");
                return false;
            }
            finally
            {
                if (lockTaken)
                {
                    Monitor.Exit(base.objLock);
                }
            }
        }


        //public bool Switch(bool bStart)
        //{

        //    byte[] bytesSend = new byte[] { 0x01, 0x0F, 0x03, 0xE7, 0x00, 0x01, 0x01, 0x01, 0xDB, 0x72 };
        //    if (!bStart)
        //    {
        //        bytesSend = new byte[] { 0x01, 0x0F, 0x03, 0xE7, 0x00, 0x01, 0x01, 0x00, 0x1A, 0xB2 };
        //    }

        //    byte[] bytesRecv = session_.sendAndReadByteLock(bytesSend,300);
        //    if (bytesRecv != null)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}


        ////设置方向 0:正转  1：反转
        //public bool SetDirection(int nDirection)
        //{

        //    byte[] bytesSend = new byte[] { 0x01, 0x0F, 0x03, 0xE8, 0x00, 0x01, 0x01, 0x01, 0x8F, 0x73 };
        //    if (nDirection == 1)
        //    {
        //        bytesSend = new byte[] { 0x01, 0x0F, 0x03, 0xE8, 0x00, 0x01, 0x01, 0x00, 0x4E, 0xB3 };
        //    }

        //    byte[] bytesRecv = session_.sendAndReadByteLock(bytesSend);
        //    if (bytesRecv != null)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}



        ////设置转速 0:50 1:200 2:500 3:3000
        //public bool SetSpeed(int nSpeed)
        //{

        //    byte[] bytesSend = new byte[] { 0x01, 0x06, 0x03, 0xE7, 0x00, 0x32, 0xB8, 0x6C };
        //    if (nSpeed == 0)
        //    {
        //        bytesSend = new byte[] { 0x01, 0x06, 0x03, 0xE7, 0x00, 0x32, 0xB8, 0x6C };
        //    }

        //    if (nSpeed == 1)
        //    {
        //        bytesSend = new byte[] { 0x01, 0x06, 0x03, 0xE7, 0x00, 0xC8, 0x38, 0x2F };
        //    }

        //    if (nSpeed == 2)
        //    {
        //        bytesSend = new byte[] { 0x01, 0x06, 0x03, 0xE7, 0x01, 0xF4, 0x39, 0xAE };
        //    }

        //    if (nSpeed == 3)
        //    {
        //        bytesSend = new byte[] { 0x01, 0x06, 0x03, 0xE7, 0x0B, 0xB8, 0x3E, 0xFB };
        //    }

        //    byte[] bytesRecv = session_.sendAndReadByteLock(bytesSend);
        //    if (bytesRecv != null)
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }
        //}






        //1 cfg spd=38400  设置运行速度 38400
        /// <summary>
        /// 电机使能
        /// </summary>
        /// <returns></returns>
        public bool ENA2()
        {
            bool temperature = false;

            try
            {
                string command = "2 ena\n";
                byte[] byteRcv = session_.sendAndReadByte(command);
                //收到FF 01 02 00 00 00 00 00 0F 7F 7F 31 70 00 00 0C 66 1F 00 38 FE
                if (byteRcv != null && byteRcv.Length == 21)
                {
                    temperature = true;
                }
            }
            catch (Exception)
            {
                temperature = false;
            }
            return temperature;
        }

        public bool ENA3()
        {
            bool temperature = false;

            try
            {
                string command = "3 ena\n";
                byte[] byteRcv = session_.sendAndReadByte(command);
                //收到FF 01 02 00 00 00 00 00 0F 7F 7F 31 70 00 00 0C 66 1F 00 38 FE
                if (byteRcv != null && byteRcv.Length == 21)
                {
                    temperature = true;
                }
            }
            catch (Exception)
            {
                temperature = false;
            }
            return temperature;
        }


        /// <summary>
        /// 补液开始  速度 19200
        /// </summary>
        /// <param name="poslocation"></param>
        /// <returns></returns>
        public bool mov2(int poslocation)
        {
            //1 mov spd = 19200
            bool temperature = false;
            try
            {
                string command = "2 mov " + poslocation + "\r\n";
                byte[] byteRcv = session_.sendAndReadByte(command);
                //收到FF 01 02 04 38 58 00 00 00 04 3F 17 40 00 00 0C 40 2F 00 68 FE 

                //FF 01 02 00 00 00 00 00 00 0A 61 42 1E 00 00 0C 42 0F 00 75 FE
                //FF 01 02 04 38 58 00 00 00 0B 17 10 73 00 00 0C 40 2F 00 7B FE
                if (byteRcv != null && byteRcv.Length == 21)
                {
                    temperature = true;
                }
            }
            catch (Exception)
            {

                temperature = false;
            }

            return temperature;

        }


        public bool mov3(int poslocation)
        {
            //1 mov spd = 19200
            bool temperature = false;
            try
            {
                string command = "3 mov " + poslocation + "\r\n";
                byte[] byteRcv = session_.sendAndReadByte(command);
                //收到FF 01 02 04 38 58 00 00 00 04 3F 17 40 00 00 0C 40 2F 00 68 FE 

                //FF 01 02 00 00 00 00 00 00 0A 61 42 1E 00 00 0C 42 0F 00 75 FE
                //FF 01 02 04 38 58 00 00 00 0B 17 10 73 00 00 0C 40 2F 00 7B FE
                if (byteRcv != null && byteRcv.Length == 21)
                {
                    temperature = true;
                }
            }
            catch (Exception)
            {

                temperature = false;
            }

            return temperature;

        }

        /// <summary>
        /// 补液停止
        /// </summary>
        /// <returns></returns>
        public bool stp2()
        {
            bool temperature = false;
            try
            {
                string command = "2 stp \r\n";
                byte[] byteRcv = session_.sendAndReadByte(command);
                //收到FF 01 02 04 38 58 00 00 00 0A 61 42 1D 00 00 0C 40 2F 00 30 FE
                if (byteRcv != null && byteRcv.Length == 21)
                {
                    temperature = true;
                }
            }
            catch (Exception)
            {

                temperature = false;
            }

            return temperature;

        }

        public bool stp3()
        {
            bool temperature = false;
            try
            {
                string command = "3 stp \r\n";
                byte[] byteRcv = session_.sendAndReadByte(command);
                //收到FF 01 02 04 38 58 00 00 00 0A 61 42 1D 00 00 0C 40 2F 00 30 FE
                if (byteRcv != null && byteRcv.Length == 21)
                {
                    temperature = true;
                }
            }
            catch (Exception)
            {

                temperature = false;
            }

            return temperature;

        }
    }
}
